package com.yangfan.screenshot.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;

import androidx.core.content.ContextCompat;

import com.yangfan.screenshot.log.LogUtil;
import com.yangfan.screenshot.service.ScreenShotAccessibilityService;

/**
 * @author : yangfan_rj
 * @date   : 2022/12/7-14:26
 * desc   : 权限工具类
 */
public final class PermissionUtil {

    private static final String TAG = PermissionUtil.class.getSimpleName();

    /**
     * 无障碍访问权限
     *
     * @param mContext m上下文
     * @return boolean
     */
    public static boolean hasAccessibilityPermission(Context mContext) {
        return hasAccessibilityPermission(mContext, ScreenShotAccessibilityService.class);
    }

    /**
     * 无障碍访问权限
     * @param mContext
     * @param serviceClass
     * @return
     */
    public static boolean hasAccessibilityPermission(Context mContext, Class serviceClass) {
        LogUtil.d(TAG,"hasServicePermission serviceClass:" + serviceClass + "；getSimpleName：" + serviceClass.getSimpleName());
        int ok = 0;
        try {
            ok = Settings.Secure.getInt(mContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }

        TextUtils.SimpleStringSplitter ms = new TextUtils.SimpleStringSplitter(':');
        if (ok == 1) {
            String settingValue = Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
            LogUtil.d(TAG,"hasAccessibilityPermission settingValue:" + settingValue);
            if (settingValue != null) {
                ms.setString(settingValue);
                while (ms.hasNext()) {
                    String accessibilityService = ms.next();
                    LogUtil.d(TAG,"hasAccessibilityPermission accessibilityService:" + accessibilityService);
                    if (accessibilityService.contains(serviceClass.getSimpleName())) {
                        LogUtil.d(TAG,"找到包含的权限");
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 跳可访问性设置
     *
     * @param context 上下文
     */
    public static void jumpAccessibilitySetting(Context context) {
        LogUtil.d(TAG, "进入无障碍设置界面");
        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 检查权限
     * @param context
     * @param strArr
     * @return
     */
    public static boolean checkPermission(Context context, String... strArr) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            LogUtil.w(TAG, "hasPermissions: API version < M, returning true by default");
            return true;
        } else if (context != null) {
            for (String str : strArr) {
                if (ContextCompat.checkSelfPermission(context, str) != 0) {
                    return false;
                }
            }
            return true;
        } else {
            throw new IllegalArgumentException("Can't check permissions for null context");
        }

    }

}
